package date;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * @author : DuJiabao
 * @Date : 2021/1/21 21:42
 * @Project : java_learning
 * @File : null.java
 * @Desc :
 */
public class TimeTest {
    @Test
    public void test() {
        long l = System.currentTimeMillis();   // 1970、1.1 0:0:0 距今的毫秒时间差
        System.out.println(l);

        Date date = new Date();  // 精确到毫秒的时间类
        System.out.println(date.getTime()); // 获取 1970、1.1 0:0:0 距今的毫秒时间差
        System.out.println(date.toString());  // Thu Jan 21 21:48:41 GMT+08:00 2021
//      格式化日期的工具类
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        System.out.println(dateFormat.format(date)); // 将Date转换成默认格式
//        指定格式
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  // 2021-01-21 10:03:19
        String format = dateFormat1.format(date);
        System.out.println(format);
//      日历类
        Calendar instance = Calendar.getInstance();
        Date time = instance.getTime();  // 获取时间
        System.out.println(time);
//        设置时间
        instance.setTime(new Date(456465745L));
        System.out.println(instance.getTime());
//        获取某个内容
        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.DAY_OF_YEAR));
//        设置某个内容
        instance.set(Calendar.YEAR, 2020);
        System.out.println(instance.getTime());
    }

    @Test
    public void test2(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
//        设置时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 3, 12, 10, 10, 10);
        System.out.println(localDateTime1);
//        获取
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getYear());
//      修改日期  返回一个新的对象，反映了不可变性
        LocalDate localDate1 = localDate.withDayOfMonth(30);
        System.out.println(localDate1);
//      修改时间，生成一个新的对象
        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime2);
//      加上指定的几个月
        LocalDate localDate2 = localDate.plusMonths(3);
        System.out.println(localDate2);
//        减指定的几天
        LocalDate localDate3 = localDate.minusDays(20);
        System.out.println(localDate3);
    }

    @Test
    public void instantTest(){
//        瞬时 类似于Date()
        Instant now = Instant.now(); //2021-01-21T14:48:46.274964100Z 这是按照格林威治时间，没有时差
        System.out.println(now);
//        添加时间的偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
//        获取对应的毫秒数
        long l = now.toEpochMilli();
        System.out.println(l);
//        构造器 根据指定的毫秒数构造instant
        Instant instant = Instant.ofEpochMilli(l);
        System.out.println(instant);
    }

    /**
     * 格式化日期时间
     */
    @Test
    public void dataTimeFormatter(){
//        预定义的标准格式
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime now = LocalDateTime.now();  // 获取当前时间
        String format = isoLocalDateTime.format(now);  // 对时间进行格式化
        System.out.println(format);
//        解析字符串
        TemporalAccessor parse = isoLocalDateTime.parse("2021-01-21T22:57:31.2892093");
        System.out.println(parse);
//        本地化相关格式 FormatStyle.SHORT FormatStyle.MEDIUM
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String format1 = dateTimeFormatter.format(now);
        System.out.println(format1);

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format2 = dateTimeFormatter1.format(LocalDate.now());
        System.out.println(format2);

//        自定义
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format3 = dateTimeFormatter2.format(LocalDateTime.now());
        System.out.println(format3);

        TemporalAccessor parse1 = dateTimeFormatter2.parse("2021-01-21 11:08:31");  // 解析字符串
        System.out.println(parse1);
    }
}
